/**
 * 
 */
package com.mc.tool;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

/**
 * 字符串处理
 * @author w.weizhen
 */
public class StringMethod {
    
    /**
     * @param args
     */
    public static void main(String[] args) {
//    	//一般的截取方法
//    	xx();
//    	
//    	//截取两个字符串之间的内容
//        substring();
    	
    	// 去除开头和结尾的特定内容
//    	beginning_end();
    	
    	// 左右补充内容，达到长度
    	fillStr();
    	
    	
    	
    	
    }
    
    //一般的截取方法
    private static void xx()
    {
        String str = "春江潮水连海平，海上明月共潮生。春江潮水连海平，海上明月共潮生。春江潮水连海平，海上明月共潮生。";
        int index = 3;
        //1 取字符串的前i个字符
        str=str.substring(0,index);
        
        //2 去掉字符串的前i个字符
        str=str.substring(index); 
        
        //3 从右边开始取i个字符
        str=str.substring(str.length()-index); 
        str=str.substring(str.length()-index,str.length()); 
        
        //4 从右边开始去掉i个字符
        str=str.substring(0,str.length()-index);
        
        //5 从开始截取到中间某个指定字符  midChar (该字符出现的第一次)
        String midChar = "5";
        str=str.substring(0,str.indexOf(midChar));
        
        //6 从开始截取到指定某段字符串结尾  midStr
        String midStr ="BVC";
        str=str.substring(0,str.indexOf(midStr)+midStr.length());
        
        //7 如果字符串中有"abc"则替换成"ABC"
        str=str.replace("abc","ABC");
        
    }
    
    //截取两个字符串之间的内容
    private static void substring()
    {
        //简单截取第一个内容,不做数据校验.
//        List<String> indexStrs = Arrays.asList("春江","明月");
//        String str = "春江潮水连海平，海上明月共潮生。春江潮水连海平，海上明月共潮生。春江潮水连海平，海上明月共潮生。";
//        String result = str.substring(str.indexOf(indexStrs.get(0)), str.indexOf(indexStrs.get(1))).substring(indexStrs.get(0).length());
//        System.out.println(result);
        
        //截取所有内容,做数据校验.
        //结果存放.
        List<String> results = new ArrayList<String>();
        
        //List<String> indexStrs = Arrays.asList("春江","明月");
        //String str = "春江潮水连海平，海上明月共潮生。春江潮水连海平，海上明月共潮生。春江潮水连海平，海上明月共潮生。春江BVC明月";
        
        //List<String> indexStrs = Arrays.asList("<ax2101:deviceCode>","</ax2101:deviceCode>");
        //String str = "<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Body><ns:getAuthListByPersonCodeResponse xmlns:ns=\"http://server.webservice.scms.wadejerry.com\"><ns:return xmlns:ax2104=\"http://pfm.support.server.webservice.scms.wadejerry.com/xsd\" xmlns:ax2106=\"http://dto.pfm.support.server.webservice.scms.wadejerry.com/xsd\" xmlns:ax299=\"http://dto.acm.support.server.webservice.scms.wadejerry.com/xsd\" xmlns:ax296=\"http://dto.onecard.support.server.webservice.scms.wadejerry.com/xsd\" xmlns:ax2109=\"http://acm.support.server.webservice.scms.wadejerry.com/xsd\" xmlns:ax294=\"http://onecard.support.server.webservice.scms.wadejerry.com/xsd\" xmlns:ax2111=\"http://model.door.acm.modules.scms.wadejerry.com/xsd\" xmlns:ax2101=\"http://support.server.webservice.scms.wadejerry.com/xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"ax2101:PersonAuthResult\"><ax294:iRet>0</ax294:iRet><ax294:strMessage xsi:nil=\"true\" /><ax2101:personAuthArr xsi:type=\"ax2101:PersonAuthDto\"><ax2101:authType>个人门授权</ax2101:authType><ax2101:deviceCode>1414496</ax2101:deviceCode><ax2101:deviceName>德生 门禁一休机</ax2101:deviceName><ax2101:doorCode>16232294_31</ax2101:doorCode><ax2101:doorName>德生 门禁一休机</ax2101:doorName><ax2101:personCode>202106080</ax2101:personCode><ax2101:updateTime>2021-06-10 10:39:50</ax2101:updateTime></ax2101:personAuthArr></ns:return></ns:getAuthListByPersonCodeResponse></soapenv:Body></soapenv:Envelope>";
        
        List<String> indexStrs = Arrays.asList("<ax294:iRet>","</ax294:iRet>");
        String str = "<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Body><ns:deletePersonResponse xmlns:ns=\"http://server.webservice.scms.wadejerry.com\"><ns:return xmlns:ax2104=\"http://pfm.support.server.webservice.scms.wadejerry.com/xsd\" xmlns:ax2106=\"http://dto.pfm.support.server.webservice.scms.wadejerry.com/xsd\" xmlns:ax299=\"http://dto.acm.support.server.webservice.scms.wadejerry.com/xsd\" xmlns:ax296=\"http://dto.onecard.support.server.webservice.scms.wadejerry.com/xsd\" xmlns:ax2109=\"http://acm.support.server.webservice.scms.wadejerry.com/xsd\" xmlns:ax294=\"http://onecard.support.server.webservice.scms.wadejerry.com/xsd\" xmlns:ax2111=\"http://model.door.acm.modules.scms.wadejerry.com/xsd\" xmlns:ax2101=\"http://support.server.webservice.scms.wadejerry.com/xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"ax294:Result\"><ax294:iRet>0</ax294:iRet><ax294:strMessage>成功</ax294:strMessage></ns:return></ns:deletePersonResponse></soapenv:Body></soapenv:Envelope>";
        
        if(StringUtils.isNotBlank(str)&&indexStrs!=null&&indexStrs.size()>0)
        if(StringUtils.isNotBlank(indexStrs.get(0))&&StringUtils.isNotBlank(indexStrs.get(1)))
        if (str.indexOf(indexStrs.get(0))>-1&&str.indexOf(indexStrs.get(1))>-1) {
            String startStr = indexStrs.get(0);
            String endStr = indexStrs.get(1);
            if(str.indexOf(indexStrs.get(0)) > str.indexOf(indexStrs.get(1)))
            {
                startStr = indexStrs.get(1);
                endStr = indexStrs.get(0);
            }
            while(StringUtils.isNotBlank(str)&&str.indexOf(startStr)>-1&&str.indexOf(endStr)>-1)
            {
                //截取两个字符串之间的内容,并存放到集合.
                String result = str.substring(str.indexOf(startStr), str.indexOf(endStr)).substring(startStr.length());
                if (StringUtils.isNotBlank(result)) {results.add(result);}
                //是否还有数据.
                str = str.substring(str.indexOf(endStr)+endStr.length());
            }
        }
        
        //System.out.println(StringUtils.join(results.toArray(), " | "));
        System.out.println(results);
    }
    
    
    private static void beginning_end()
    {
    	// 去掉 字符串  开头的 0
    	String s = "000123000";
    	//方法1：String ——> Integer
    	//String str = String.valueOf(Integer.parseInt(s));
    	//方法2：正则
    	//String str = s.replaceAll("^(0+)", "");
    	//方法3：匹配开头
    	String str = s.replaceFirst("0*", "");
    	System.out.println(str);
    	
    	// 去掉 字符串  末尾的 0
    	String str2 = s.replaceAll("(00)+$", "");
    	System.out.println(str2);
    	
    	System.out.println(s.substring(0, 6));
    }
    
    // 左右补充内容，达到长度
    private static void fillStr()
    {
    	int i = 7;
    	System.out.println(String.format("%06d", i));
    	System.out.println(String.format("%-7s", i+i).replace(" ", "0"));
    	
    	String str = "xx";// 原始内容
    	int size = 5;// 内容长度
    	String padStr = "0";// 填补内容
    	System.out.println(StringUtils.rightPad(str, size, padStr));// 向右填补
    	System.out.println(StringUtils.leftPad(str, size, padStr));// 向左填补
    }
    
    
    
}